<template>
	<view>
		<!-- 用户信息表单 -->
		<uni-forms :modelValue="user">
			<uni-forms-item label="用户名" name="username">
				<uni-easyinput type="text" v-model="user.username" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="密码" name="password">
				<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>

			<button :plain="false" @click="login()">登录</button>
			<button :plain="false" @click="reg()">去注册</button>
		</uni-forms>
		<view>{{message}}</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					username: '',
					password: '',
				},
				message: '',
				result: false,
			}
		},
		methods: {
			back() {
				uni.navigateBack({

				})
			},
			login() { //登录
				uni.request({
					url: 'http://localhost:8081/user/login',
					method: 'POST',
					data: this.user,
					success: (res) => {
						if (!res.data.result) {
							this.message = "用户名或者密码错误"
						} else {
							this.result = res.data.result;
							uni.setStorage({
								key: 'token',
								data: this.user.username,
							});
							this.message = '';
							uni.switchTab({
								url: 'user'
							})
						}
					},
				});
			},
			reg() {
				uni.navigateTo({
					url: 'register', //前往注册页面
				})
			}
		},

	}
</script>

<style>

</style>
